﻿using Decoration.IService;
using Decoration.Models.Models;
using Decoration.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Decoration.Comm;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Decoration.Models.Dto;
using AutoMapper;

namespace Decoration.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class WF_ApprovalFlowController : ControllerBase
    {
        IWF_ApprovalFlowService svc;
        public WF_ApprovalFlowController(IWF_ApprovalFlowService _svc) 
        {
            this.svc = _svc;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="TypeName"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        public PagedResult<WF_ApprovalFlow> Query(string? TypeName, int PageSize = 10, int PageIndex = 1)
        {
            string sql = "select *from WF_ApprovalFlows where 1=1";
            if (!TypeName.IsNullOrEmpty())
            {
                sql += " and TypeName like @TypeName";
            }
            //查询总条数
            string countSql = "select count(*) num from (" + sql + ") a";
            int total = svc.ExecuteScalar<int>(sql, new { TypeName });

            sql += "  order by Id offset @PageSize*(@PageIndex-1) rows fetch next @PageSize rows only";
            var data = svc.Query<WF_ApprovalFlow>(sql, new { TypeName, PageSize, PageIndex });

            return new PagedResult<WF_ApprovalFlow>() { code = 1, msg = "成功", total = total, data = data };
        }
        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public Result Get(int id)
        {
            var data = svc.Get(o => o.Id == id);

            if (data == null) return new Result() { code = -1, msg = "数据不存在" };

            return new Result() { code = 1, msg = "成功", data = data };
        }

        /// <summary>
        /// 新增订单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Post(WF_ApprovalFlow info)
        {
            var res = svc.Add(info);
            return new Result() { code = 1, msg = "成功", data = res };
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public Result Put(WF_ApprovalFlow dto)
        {
            var data = svc.Get(o => o.Id == dto.Id);

            if (data == null) return new Result() { code = -1, msg = "数据不存在" };
            data.Status = dto.Status;
            data.TypeCode= data.TypeCode;
            data.TypeDesc = dto.TypeDesc;
            data.TypeName = dto.TypeName;

            if (svc.Update(data))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result Delete(int id)
        {
            var data = svc.Get(o => o.Id == id);

            if (data == null) return new Result() { code = -1, msg = "数据不存在" };

            data.Status = 0;

            if (svc.Update(data))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }

        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns></returns>
        [HttpGet("getAll")]
        public Result GetAll()
        {
            var data = svc.Query(o => true);

            return new Result() { code=1,msg="成功",data = data};
        }
    }
}
